// app/service/user.js
const BaseService = require('./base')
const { Op } = require('sequelize')
class ArticalService extends BaseService {
	constructor(...args) {
		super(...args)
		this.modelName = 'Artical'
	}
	async updateList(arr) {
		let count = 0
		for (let i = 0; i < arr.length; i++) {
			let user = arr[i]
			const u = await this.ctx.model[this.modelName].findOne({
				where: {
					title: {
						[Op.eq]: user.title,
					},
				},
			})
			// 没找到了数据证明不存在相同信息
			if (!u) {
				// 创建用户
				let res = await this.ctx.model[this.modelName].create({
					category_id: 3,
					...user,
				})
				if (res) count++
			} else {
				// 找到了数据证明存在，不能进行添加
				continue
			}
		}
		return { flag: true, count }
	}
	async getArticelById(id) {
		return await this.ctx.model[this.modelName].findOne({
			where: {
				id: {
					[Op.eq]: id,
				},
			},
			distinct: true,
			include: [
				{
					model: this.ctx.model.Categorical,
				},
			],
		})
	}
}

module.exports = ArticalService
